package com.relsell.view.phone;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.ListView;
import android.widget.Toast;

import com.relsell.controller.ExpandableListAdapter;
import com.relsell.model.NavDrawerItem;
import com.relsell.tmcprototype.R;
import com.relsell.utility.ApplicationVars;

public class RegularLessons extends FragmentActivity {

	private DrawerLayout mDrawerLayout;
	// private ListView mDrawerList;
	private ActionBarDrawerToggle mDrawerToggle;

	// nav drawer title
	private CharSequence mDrawerTitle;

	// used to store app title
	private CharSequence mTitle;

	// slide menu items
	private String[] navMenuTitles;
	private TypedArray navMenuIcons;

	private ArrayList<NavDrawerItem> navDrawerItems;
	// private NavDrawerListAdapter adapter;

	ExpandableListAdapter listAdapter;
	ExpandableListView expListView;
	List<String> listDataHeader;
	HashMap<String, List<String>> listDataChild;
	protected int lastExpandedGroupPosition = -1;
	public static final int Guitar_LESSONS_BEGINNER_LESSONS = 101;
	public static final int Guitar_LESSONS_CHORDS = 102;
	public static final int Guitar_LESSONS_TOPIC_BASED = 103;
	public static final int Guitar_LESSONS_SONG_LESSON = 104;
	public static final int Guitar_LESSONS_STYLE_GENRE = 105;
	public static final int Guitar_LESSONS_SPEED = 106;
	public static final int Guitar_LESSONS_LEAD_GUITAR = 107;
	
	public static final int Bass_LESSON_Beginner_Lessons = 201;
	public static final int Bass_LESSON_Intermediate = 202;
	public static final int Bass_LESSON_Style = 203;
	public static final int Bass_LESSON_Song_Lessons = 204;
	
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.regular_lessons);

		mTitle = mDrawerTitle = getTitle();

		// load slide menu items
		navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);

		// nav drawer icons from resources
		navMenuIcons = getResources()
				.obtainTypedArray(R.array.nav_drawer_icons);

		mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

		// get the listview
		expListView = (ExpandableListView) findViewById(R.id.list_slidermenu);
		// mDrawerList = (ListView) findViewById(R.id.list_slidermenu);

		// preparing list data
		prepareListData();

		listAdapter = new ExpandableListAdapter(this, listDataHeader,
				listDataChild);

		navDrawerItems = new ArrayList<NavDrawerItem>();

		// adding nav drawer items to array
		// Home
		navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons
				.getResourceId(0, -1)));
		// Find People
		navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons
				.getResourceId(1, -1)));
		// Photos
		navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons
				.getResourceId(2, -1)));

		/*
		 * listAdapter = new ExpandableListAdapter(this, navDrawerItems,
		 * listDataChild);
		 */

		// Recycle the typed array
		navMenuIcons.recycle();

		ApplicationVars.getInstance().setupActionBar(RegularLessons.this,getActionBar(),false, false, true, true, true);

		mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
				R.drawable.ic_drawer, // nav menu toggle icon
				R.string.app_name, // nav drawer open - description for
									// accessibility
				R.string.app_name // nav drawer close - description for
									// accessibility
		) {
			public void onDrawerClosed(View view) {
				getActionBar().setTitle(mTitle);
				// calling onPrepareOptionsMenu() to show action bar icons
				invalidateOptionsMenu();
			}

			public void onDrawerOpened(View drawerView) {
				getActionBar().setTitle(mDrawerTitle);
				// calling onPrepareOptionsMenu() to hide action bar icons
				invalidateOptionsMenu();
			}
		};
		mDrawerLayout.setDrawerListener(mDrawerToggle);

		if (savedInstanceState == null) {
			displayView(0);
		}

		// setting list adapter
		expListView.setAdapter(listAdapter);

		// Listview Group click listener
		expListView.setOnGroupClickListener(new OnGroupClickListener() {

			@Override
			public boolean onGroupClick(ExpandableListView parent, View v,
					int groupPosition, long id) {
				boolean result = false;
				if (groupPosition == 0) {
					result = false;
				} else if (groupPosition == 1) {
					result = false;
				} else if (groupPosition == 2) {
					displayView(2);
					result = true;
				} else if (groupPosition == 3) {
					displayView(3);
					result = true;
				} else if (groupPosition == 4) {
					//displayView(4);
					result = true;
				} else if (groupPosition == 5) {
					result = false;
				}

				return result;
			}
		});

		// Listview Group expanded listener
		expListView.setOnGroupExpandListener(new OnGroupExpandListener() {

			@Override
			public void onGroupExpand(int groupPosition) {
				if(lastExpandedGroupPosition != -1
	                    && groupPosition != lastExpandedGroupPosition){
		        	expListView.collapseGroup(lastExpandedGroupPosition);
		        }
		        lastExpandedGroupPosition  = groupPosition;
			}
		});

		// Listview Group collasped listener
		expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {

			@Override
			public void onGroupCollapse(int groupPosition) {
				Toast.makeText(getApplicationContext(),
						listDataHeader.get(groupPosition) + " Collapsed",
						Toast.LENGTH_SHORT).show();

			}
		});

		// Listview on child click listener
		expListView.setOnChildClickListener(new OnChildClickListener() {

			@Override
			public boolean onChildClick(ExpandableListView parent, View v,
					int groupPosition, int childPosition, long id) {
				boolean result = false;
				
				if (groupPosition == 0 && childPosition == 0) {
					displayView(RegularLessons.Guitar_LESSONS_BEGINNER_LESSONS);
					return true;
				} else if (groupPosition == 0 && childPosition == 1) {
					displayView(RegularLessons.Guitar_LESSONS_CHORDS);
					return true;
				} else if (groupPosition == 0 && childPosition == 2) {
					displayView(RegularLessons.Guitar_LESSONS_TOPIC_BASED);
					return true;
				} else if (groupPosition == 0 && childPosition == 3) {
					displayView(RegularLessons.Guitar_LESSONS_SONG_LESSON);
					return true;
				} else if (groupPosition == 0 && childPosition == 4) {
					displayView(RegularLessons.Guitar_LESSONS_STYLE_GENRE);
					return true;
				} else if (groupPosition == 0 && childPosition == 5) {
					displayView(RegularLessons.Guitar_LESSONS_SPEED);
					return true;
				} else if (groupPosition == 0 && childPosition == 6) {
					displayView(RegularLessons.Guitar_LESSONS_LEAD_GUITAR);
					return true;
				} else if (groupPosition == 1 && childPosition == 0) {
					displayView(RegularLessons.Bass_LESSON_Beginner_Lessons);
					return true;
				} else if (groupPosition == 1 && childPosition == 1) {
					displayView(RegularLessons.Bass_LESSON_Intermediate);
					return true;
				} else if (groupPosition == 1 && childPosition == 2) {
					displayView(RegularLessons.Bass_LESSON_Style);
					return true;
				} else if (groupPosition == 1 && childPosition == 3) {
					displayView(RegularLessons.Bass_LESSON_Song_Lessons);
					return true;
				}

				return result;
			}
		});
	}

	/**
	 * Slide menu item click listener
	 * */
	private class SlideMenuClickListener implements
			ListView.OnItemClickListener {
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			// display view for selected nav drawer item
			displayView(position);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// toggle nav drawer on selecting action bar app icon/title
		if (mDrawerToggle.onOptionsItemSelected(item)) {
			return true;
		}
		// Handle action bar actions click
		switch (item.getItemId()) {
		case R.id.action_contactus:
			startActivity(new Intent(RegularLessons.this, ContactUs.class));
			return true;
		case R.id.action_search:
			showCustomDialog(RegularLessons.this);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	public void showCustomDialog(Context context) {
		// custom dialog
		View v = RegularLessons.this.getLayoutInflater().inflate(
				R.layout.custom_dialog, null);
		final AlertDialog dialog = new AlertDialog.Builder(context).setView(v)
				.create();

		WindowManager.LayoutParams wmlp = dialog.getWindow().getAttributes();

		wmlp.gravity = Gravity.TOP | Gravity.LEFT;
		wmlp.x = 0; // x position
		wmlp.y = 0; // y position
		wmlp.width = WindowManager.LayoutParams.MATCH_PARENT;
		dialog.show();
		dialog.getWindow().setAttributes(wmlp);
	}

	/* *
	 * Called when invalidateOptionsMenu() is triggered
	 */
	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		// if nav drawer is opened, hide the action items
		boolean drawerOpen = mDrawerLayout.isDrawerOpen(expListView);
		menu.findItem(R.id.action_contactus).setVisible(!drawerOpen);
		return super.onPrepareOptionsMenu(menu);
	}

	/**
	 * Diplaying fragment view for selected nav drawer list item
	 * */
	private void displayView(int position) {
		// update the main content by replacing fragments
		Fragment fragment = null;
		switch (position) {
		case 0:
		case RegularLessons.Guitar_LESSONS_BEGINNER_LESSONS:
		case RegularLessons.Guitar_LESSONS_CHORDS:
		case RegularLessons.Guitar_LESSONS_TOPIC_BASED:
		case RegularLessons.Guitar_LESSONS_SONG_LESSON:
		case RegularLessons.Guitar_LESSONS_STYLE_GENRE:	
		case RegularLessons.Guitar_LESSONS_SPEED:
		case RegularLessons.Guitar_LESSONS_LEAD_GUITAR:	
		case RegularLessons.Bass_LESSON_Beginner_Lessons:	
		case RegularLessons.Bass_LESSON_Intermediate:	
		case RegularLessons.Bass_LESSON_Style:	
		case RegularLessons.Bass_LESSON_Song_Lessons:		
			fragment = new GuitarLessons();
			break;
		case 1:
			fragment = new BassLessons();
			break;
		case 2:
			fragment = new SongsLessons();
			break;
		case 3:
			fragment = new InstructorsFragment();
			break;
		case 4:
			fragment = new UpcomingLessonsFragment();
			break;

		default:
			break;
		}

		if (fragment != null) {
			FragmentManager fragmentManager = getSupportFragmentManager();
			fragmentManager.beginTransaction()
					.replace(R.id.frame_container, fragment).commit();

			// update selected item and title, then close the drawer
			expListView.setItemChecked(position, true);
			expListView.setSelection(position);
			mDrawerLayout.closeDrawer(expListView);
		} else {
			// error in creating fragment
			Log.e("MainActivity", "Error in creating fragment");
		}
	}

	@Override
	public void setTitle(CharSequence title) {
		mTitle = title;
		getActionBar().setTitle(mTitle);
	}

	/**
	 * When using the ActionBarDrawerToggle, you must call it during
	 * onPostCreate() and onConfigurationChanged()...
	 */

	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		super.onPostCreate(savedInstanceState);
		// Sync the toggle state after onRestoreInstanceState has occurred.
		mDrawerToggle.syncState();
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		// Pass any configuration change to the drawer toggls
		mDrawerToggle.onConfigurationChanged(newConfig);
	}

	private void prepareListData() {
		listDataHeader = new ArrayList<String>();
		listDataChild = new HashMap<String, List<String>>();

		// Adding child data
		listDataHeader.add("Guitar Lessons");
		listDataHeader.add("Bass Lessons");
		listDataHeader.add("Songs Lessons");
		listDataHeader.add("Instructors");
		listDataHeader.add("Upcoming Lessons");
		listDataHeader.add("Free Lessons");

		// Adding child data
		List<String> GuitarLessons = new ArrayList<String>();
		GuitarLessons.add("Beginners Lessons");
		GuitarLessons.add("Chord & Rhythm");
		GuitarLessons.add("Topic Based");
		GuitarLessons.add("Song Lesson");
		GuitarLessons.add("Style / Genre");
		GuitarLessons.add("Speed & Shredding");
		GuitarLessons.add("Lead Guitar");

		List<String> BassLessons = new ArrayList<String>();
		BassLessons.add("Beginner Lessons");
		BassLessons.add("Intermediate Lessons");
		BassLessons.add("Style");
		BassLessons.add("Song Lessons");
		
		List<String> SongsLessons = new ArrayList<String>();
		SongsLessons.add("2 Guns");
		SongsLessons.add("The Smurfs 2");
		SongsLessons.add("The Spectacular Now");
		SongsLessons.add("The Canyons");
		SongsLessons.add("Europa Report");

		List<String> Instructors = new ArrayList<String>();

		List<String> upcomingLessons = new ArrayList<String>();
		upcomingLessons.add("Lesson 1");
		upcomingLessons.add("Lesson 2");
		
		List<String> free = new ArrayList<String>();
		free.add("Lesson 1");
		free.add("Lesson 2");

		listDataChild.put(listDataHeader.get(0), GuitarLessons);
		listDataChild.put(listDataHeader.get(1), BassLessons);
		listDataChild.put(listDataHeader.get(2), SongsLessons);
		listDataChild.put(listDataHeader.get(3), Instructors);
		listDataChild.put(listDataHeader.get(4), upcomingLessons);
		listDataChild.put(listDataHeader.get(5), free);
	}

}
